package dao;

import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;

import bean.ClearMachine;
import util.DBUtil;

public class ClearMachineDao {
  DBUtil db=new DBUtil();
  Connection conn=db.open();
  PreparedStatement pstmt=null;
  ResultSet rs=null;
  public ArrayList<ClearMachine> getAllMachine(){
	  ArrayList<ClearMachine> list=new ArrayList<ClearMachine>();
	  String Find="select * from clearMachine";
	  try {
		pstmt=conn.prepareStatement(Find);
		rs=pstmt.executeQuery();
		while(rs.next()){
			int id=rs.getInt(1);
			String lowid=rs.getString(2);
			String name=rs.getString(3);
			String type=rs.getString(4);
			String producer=rs.getString(5);
			String version=rs.getString(6);
			Date setTime=rs.getDate(7);
			
			 ClearMachine cm=new ClearMachine();
			 cm.setId(id);
			 cm.setLowid(lowid);
			 cm.setName(name);
			 cm.setType(type);
			 cm.setProducer(producer);
			 cm.setVersion(version);
			 cm.setSetTime(setTime);
			  list.add(cm);
		}
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		db.close();
	}	  
	  return list;
  }
  
  //添加净化器信息
  public void addClearMachine(ClearMachine cm){
	  String Insert="insert into clearMachine(low_id,low_name,type,producer,product_version,set_time) values(?,?,?,?,?,?)";
	  try {
		pstmt=conn.prepareStatement(Insert);
		pstmt.setString(1, cm.getLowid());
		pstmt.setString(2, cm.getName());
		pstmt.setString(3, cm.getType());
		pstmt.setString(4, cm.getProducer());
		pstmt.setString(5, cm.getVersion());
		pstmt.setDate(6, new java.sql.Date((cm.getSetTime()).getTime()));
		pstmt.executeUpdate();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		db.close();
	}	  
  }
  //删除净化器信息
  public void deleClear(int id){
	  String DELETE="delete from clearMachine where id="+id;
	  try {
		pstmt=conn.prepareStatement(DELETE);
		pstmt.executeUpdate();
	} catch (SQLException e) {
		// TODO Auto-generated catch block
		e.printStackTrace();
	}finally{
		db.close();
	}
	  
  }
}
